package com.org.khtn.stringcommand;

import java.util.Vector;

public class Format extends AbstractFormat {
	public String commandString = null;
	Vector<String> StringMarker;

	public String replace(String src, String rep) {
		return commandString.replace(src, rep);
	}

	// tim cac field or table trong dau
	public Vector findStringIn(String reg) {
		Vector<String> temp = new Vector<>();
		// // // System.out.println("replace()");
		String result = commandString;
		int length = commandString.length();
		for (int i = 0; i < length - 1; i++) {
			// // System.out.println("replace() i="+i+commandString.charAt(i));
			if (commandString.charAt(i) == reg.charAt(0)) {
				// // // System.out.println("Tim thay ");
				for (int j = i + 1; j < length; j++) {
					// // System.out.print(commandString.charAt(j));
					// // // System.out.println(j+" "+commandString.charAt(j));
					if (commandString.charAt(j) == reg.charAt(1)) {
						String tam = result.substring(i + 1, j);
						// // // System.out.println("tam : " + tam);
						// // // System.out.println("truoc : " + result);
						// result = result.replaceFirst(tam, "{" + nString +
						// "}");
						temp.add(tam);
						// // // System.out.println("sau : " + result);
						length = result.length();
						// // // System.out.println("dai sau replace "+length);
						// // System.out.println(result.charAt(i));
						// // // System.out.println(result.charAt(length-2));
						i = j;
						break;

						// return result;
					}
				}
				// //// // System.out.println("replace " + nString);
			}
		}
		return temp;
	}

	// public xoa ngoac thua

	public Vector findStringIn1(String reg) {
		Vector<String> temp = new Vector<>();
		// // // System.out.println("replace()");
		String result = commandString;
		int length = commandString.length();
		for (int i = 0; i < length - 1; i++) {
			// // System.out.println("replace() i="+i+commandString.charAt(i));
			if (commandString.charAt(i) == reg.charAt(0)) {
				// // // System.out.println("Tim thay ");
				Boolean flags = false;
				int nN = 1;
				int nI = 0;
				for (int j = i + 1; j < length; j++) {
					// // System.out.print(commandString.charAt(j));
					// // // System.out.println(j+" "+commandString.charAt(j));
					if (commandString.charAt(j) == reg.charAt(0))
						nN++;
					if (commandString.charAt(j) == reg.charAt(1)) {
						nI++;
						if (nN == nI) {
							flags = true;
							String tam = result.substring(i + 1, j);

							// System.out.println("tam : " + tam);
							// // // System.out.println("truoc : " + result);
							// result = result.replaceFirst(tam, "{" + nString +
							// "}");
							temp.add(tam);
							// // // System.out.println("sau : " + result);
							length = result.length();
							// // //
							// System.out.println("dai sau replace "+length);
							// // System.out.println(result.charAt(i));
							// // //
							// System.out.println(result.charAt(length-2));
							// i = j;
							break;
						}
						// return result;
					}
				}
				if (flags == false) {

					return null;
				}
				// //// // System.out.println("replace " + nString);
			}
		}
		return temp;
	}

	// Doi tu marker ve lai chuoi binh thuong
	public String replaceMarker() {
		int nMarker = StringMarker.size();
		String result = commandString;
		for (int i = 0; i < nMarker; i++) {
			commandString = commandString.replace("{" + i + "}",
					StringMarker.get(i));
		}
		String tam1 = commandString;
		commandString = result;
		result = tam1;

		return result;

	}

	// Ham final xu ly chuoi
	public void setCommandString(String command) {
		commandString = command;
	}

	public Vector getStringMarker() {
		return StringMarker;
	}

	@Override
	public String XLC() {
		Vector<String> tam = findStringIn("[]");
		StringMarker = tam;
		String result = commandString;
		commandString = trim();
		for (int i = 0; i < tam.size(); i++) {
			commandString = replace(tam.get(i), "{" + i + "}");
		}
		commandString = trimAllString();
		// commandString = replaceMarker();
		commandString = replace(", ", ",");
		commandString = replace(" ,", ",");

		commandString = replace("( ", "(");
		commandString = replace(" )", ")");
		commandString = replace(" =", "=");
		commandString = replace("= ", "=");
		commandString = themNgoacTron("WHERE", new String[] { "GROUP BY",
				"ORDER BY" });
		commandString = replace("GROUP BY", "GROUPBY");
		commandString = replace("ORDER BY", "ORDERBY");

		String tam1 = commandString;
		commandString = result;
		result = tam1;
		result = result;
		return result;

	}

	public String themNgoacTron(String pre, String[] post) {
		String result = commandString;
		if (commandString.contains(pre)) {

			int length = post.length;
			int i = 0;
			Boolean flags = false;
			int idxPre = commandString.indexOf(pre);
			idxPre += pre.length() + 1; // tinh sau tu where
			// System.out.println("Format : themNgoacTron() : idxPre = "+idxPre);

			for (; i < length; i++) {
				if (commandString.contains(post[i])) {
					flags = true;
					break;
				}
			}
			int idxPost = -1;
			if (flags) {
				idxPost = commandString.indexOf(post[i]);
				idxPost -= 2;

			} else {
				idxPost = commandString.length() - 1;
			}
			// System.out.println("Format : themNgoacTron() : commandString.length = "+commandString.length());

			// System.out.println("Format : themNgoacTron() : idxPost = "+idxPost);

			String temp = commandString.substring(idxPre, idxPost + 1);
			// System.out.println("Format : themNgoacTron() : temp = "+temp);

			commandString = commandString.replace(temp, '(' + temp + ')');
			// System.out.println("Format : themNgoacTron() : temp = "+temp);

		}
		String tam = commandString;
		commandString = result;
		result = tam;
		return result;
	}

	public int replaceSTT() {
		// System.out.println("Format : replaceSTT() ");

		StringMarker = findStringIn1("()");
		// //
		// System.out.println("Format : replaceSTT() : length(StringMarker) = "+StringMarker.size()
		// );
		int length = StringMarker.size();

		if (length == 0)
			return -1;
		// System.out.println("Format : replaceSTT() : length(StringMarker) = "
		// + length);
		int i = 0;
		if (commandString.charAt(0) == '(')
			i = 1;
		for (; i < length; i++)
			commandString = replace('(' + StringMarker.get(i) + ')', "|" + i
					+ "|");
		commandString = commandString.toUpperCase();

		// System.out.println("Format : replaceSTT() : commandString = "+
		// commandString);
		return 1;
	}

	public Format() {
		StringMarker = new Vector<String>();

	}

	public boolean findObject(String a, String[] b) {
		int idx1 = commandString.indexOf(b[0]);
		int idx2 = commandString.indexOf(b[1]);
		if (idx1 < 0 || idx2 < 0)
			return false;
		// System.out.println("Format : findobject() : idx1 "+ idx1);
		// System.out.println("Format : findobject() : idx2 "+ idx2);
		idx1 += b[0].length();
		String sub = commandString.substring(idx1, idx2);
		if (sub.contains(a))
			return true;
		return false;

	}

	@Override
	public String trimAllString() {
		// TODO Auto-generated method stub
		String result = commandString;
		result = trim();
		int i = -1;
		int length = result.length();
		while (i < length - 1) {
			length = result.length();

			i++;

			if (result.charAt(i) == ' ') {
				for (int j = i + 1; j < length; j++) {
					if (result.charAt(j) != ' ')
						break;

					result = result.replace("  ", " ");
					i = 0;
					break;

				}

			}

		}

		return result;
	}

	// xoa khoan trang 2 ben chuoi
	public String trim() {
		return commandString.trim();
	}

	public Format(String command) {
		commandString = command;
		StringMarker = new Vector<String>();
	}

	// public static void main(String args[]) {
	// String command =
	// "[MaLoaiTai    Khoan]           FROM              [LoaiTaiKhoan        ASA]";
	// command = command.trim();
	// Format f = new Format();
	// f.commandString = command;
	// // // System.out.println(f.XLC());
	//
	// }

}
